package com.person.jiaiplan.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.person.jiaiplan.assembler.ClockLogsAssembler;
import com.person.jiaiplan.pojo.R;
import com.person.jiaiplan.pojo.dto.ClockInReqDTO;
import com.person.jiaiplan.pojo.dto.ClockLogsResDTO;
import com.person.jiaiplan.pojo.entity.Clocklogs;
import com.person.jiaiplan.service.ClocklogsService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * 打卡记录
 */
@AllArgsConstructor
@RestController
@RequestMapping("clock")
@Validated
public class ClockLogsController {

    @Autowired
    private ClocklogsService clocklogsService;

    /**
     * 下拉框列表获取
     *
     * @param
     * @return
     */
    @GetMapping("query")
    public R<List<ClockLogsResDTO>> queryByScheduleId(@RequestParam(name = "scheduleId") String scheduleId) {
        List<Clocklogs> clocklogsList = clocklogsService.list(Wrappers.<Clocklogs>lambdaQuery()
                .eq(Clocklogs::getScheduleId, scheduleId)
                .eq(Clocklogs::getClockType, 1)
                .orderByAsc(Clocklogs::getClockTime));
        return R.ok(ClockLogsAssembler.INSTANCE.toClockLogsResDTOList(clocklogsList));
    }

    /**
     * 查询今日打卡记录
     *
     * @param scheduleId
     * @return
     */
    @GetMapping("query/today")
    public R<ClockLogsResDTO> queryTodayClockIn(@RequestParam(name = "scheduleId") String scheduleId) {
        Clocklogs clocklogs = clocklogsService.getOne(Wrappers.<Clocklogs>lambdaQuery()
                .eq(Clocklogs::getScheduleId, scheduleId)
                .eq(Clocklogs::getClockTime, DateUtil.beginOfDay(new Date()))
                .eq(Clocklogs::getClockType, 1));
        return R.ok(ClockLogsAssembler.INSTANCE.clocklogsToClockLogsResDTO(clocklogs));
    }


    @PostMapping("save")
    public R<Boolean> save(@RequestBody ClockInReqDTO req) {
        System.out.println(req);
        return R.ok(clocklogsService.saveClock(req));
    }


    /**
     * 打卡+ 上传文件
     *
     * @param
     * @return
     */
    @PostMapping("clockIn")
    public R<Boolean> clockIn(Date clockTime, String scheduleId, Double useageHour, String remark,String clockType, @RequestParam MultipartFile file) {
        ClockInReqDTO clockInReqDTO = new ClockInReqDTO();
        clockInReqDTO.setClockTime(clockTime);
        clockInReqDTO.setScheduleId(scheduleId);
        clockInReqDTO.setUseageHour(useageHour);
        clockInReqDTO.setRemark(remark);
        clockInReqDTO.setClockType(Integer.valueOf(clockType));
        clockInReqDTO.setFile(file);
        return R.ok(clocklogsService.clockIn(clockInReqDTO));
    }
}
